package com.xr.oa.announce;

import com.bstek.dorado.data.*;
import com.bstek.dorado.data.db.SqlDataset;
import com.bstek.dorado.common.*;
import com.xr.common.Tools;

/**
 * datasetListener
 */
public class datasetListener extends AbstractDatasetListener {
  public boolean beforeLoadData(Dataset dataset)
     throws Exception {
	  String className = dataset.parameters().getString("className");
		String sql = "";
		SqlDataset ds = (SqlDataset) dataset;
		if (className != null && className.equals("datasetDept")) { //
			String infoAnnounceId=Tools.toStr(dataset.parameters().getString("info_announce_id"));
			String superDeptId=Tools.toStr(dataset.parameters().getString("super_dept_id"));
			if (!superDeptId.equals("") &&  !superDeptId.equals("null"))  {
				superDeptId="p.super_dept_id="+superDeptId;
			}else{
				superDeptId="p.super_dept_id is null";
			}
			sql="select p.provider_id,p.provider_name,p.SUPER_DEPT_ID,1 isChecked from pb_provider p,oa_info_dept_role idr " +
					"where p.is_valid=1 and p.type=1 " +
					"and p.provider_id=idr.dept_role_id and idr.type=1 and p.is_valid=1 and idr.info_announce_id="+infoAnnounceId +
					" and "+superDeptId+" union " +
					"select p.provider_id,p.provider_name,p.SUPER_DEPT_ID,0 isChecked from pb_provider p where " +
					"p.is_valid=1 and p.type=1 and "+superDeptId+
					" and p.provider_id not in (select dept_role_id from oa_info_dept_role " +
					"where info_announce_id="+infoAnnounceId+" and type=1 )";
			ds.setSql(sql);
		}
		
		if (className != null && className.equals("datasetRole")) { //
			String infoAnnounceId=Tools.toStr(dataset.parameters().getString("info_announce_id"));
			sql="select * from (select rri.role_ins_id,rri.role_name role_name_ins,rri.role_desc,rri.role_name role_name_def,1 isChecked" +
					" from RT_ROLE_INS rri,oa_info_dept_role idr where " +
					" rri.role_ins_id=idr.dept_role_id and idr.type=2 and idr.info_announce_id="+infoAnnounceId +
					" union " +
					" select rri.role_ins_id,rri.role_name role_name_ins,rri.role_desc,rri.role_name role_name_def,0 isChecked" +
					" from RT_ROLE_INS rri,rt_role_def rrd where " +
					" rri.role_ins_id not in(select dept_role_id from oa_info_dept_role " +
					"where info_announce_id="+infoAnnounceId+" and type=2 )) order by role_ins_id";
			ds.setSql(sql);
			ds.setCountSql("select count(*) from ("+sql+")");
			
		}

		
    return true;
  }

}
